package com.linkturing.swing;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TooManyListenersException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import com.alibaba.fastjson.JSON;
import com.linkturing.serial.AddWeightBean;
import com.linkturing.serial.SerialRead;
import com.linkturing.util.BizHelper;
import com.linkturing.util.Constants;
import com.linkturing.util.JedisUtil;
import com.linkturing.util.JsonResult;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import gnu.io.NoSuchPortException;

public class XinhuaApplication {
	
//	Log log = Log.get(XinhuaApplication.class);
	static Log log = LogFactory.get();
	
	public static String serail_number;
	
	private static JFrame frmv;
	public static JTextField txt1;
	public static JTextField textField;
	private static JTextField textField_1;
	private static JTextField textField_2;
	public static JTextField textField_3;
	private static JTextField textField_4;
	private static JTextField textField_5;
	private static JTextField textField_6;
	//卷号
	public static JLabel lblNewLabel_9;

	//操作员
	public static JLabel lblNewLabel_1_1;
	
	public static JScrollPane scrollPane = new JScrollPane();
	public static JTable table;
	public static DataTable dataTable = null;
	
	public static int machine_id = 1;
	static double lastValue = 0;
	static List<Double> datalst = new LinkedList<Double>();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		try {
			XinhuaApplication window = new XinhuaApplication();
			window.frmv.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		/*EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					XinhuaApplication window = new XinhuaApplication();
					window.frmv.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});*/
	}

	/**
	 * Create the application.
	 * @throws NoSuchPortException 
	 * @throws TooManyListenersException 
	 */
	public XinhuaApplication() throws TooManyListenersException, NoSuchPortException {
		
		initialize();
		dataTable = new DataTable(txt1.getText()+":"+Constants.weight+":*");
		dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":*");
		
		try {
			log.debug("初始化称重机......");
			SerialRead.process();
		} catch(Exception ex) {
			log.error("称重机串口读取异常，请检查！");
		}
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmv = new JFrame();
		frmv.setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\gangjiaoxian\\logo.png"));
		//张家港新华预应力钢绞线有限公司V1.0
		frmv.setTitle("\u5F20\u5BB6\u6E2F\u65B0\u534E\u9884\u5E94\u529B\u94A2\u7EDE\u7EBF\u6709\u9650\u516C\u53F8V1.1");
		frmv.setBounds(100, 100, 1363, 896);
		frmv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmv.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(10, 31, 805, 269);
		frmv.getContentPane().add(panel);
		panel.setLayout(null);

		//计量日期：
		JLabel lblNewLabel = new JLabel("\u8BA1\u91CF\u65E5\u671F\uFF1A");
		lblNewLabel.setBounds(0, 34, 79, 33);
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(lblNewLabel);

		//当前  分卷机：
		JLabel lblNewLabel_6 = new JLabel("\u5F53\u524D  \u5206\u5377\u673A\uFF1A");
		lblNewLabel_6.setFont(new Font("宋体", Font.PLAIN, 14));
		lblNewLabel_6.setBounds(26, 310, 102, 48);
		frmv.getContentPane().add(lblNewLabel_6);
		JLabel lblNewLabel_7 = new JLabel("1#");
		lblNewLabel_7.setForeground(Color.RED);
		lblNewLabel_7.setFont(new Font("宋体", Font.BOLD, 24));
		lblNewLabel_7.setBounds(142, 310, 42, 48);
		frmv.getContentPane().add(lblNewLabel_7);

		//称重卷号：
		JLabel lblNewLabel_8 = new JLabel("\u79F0\u91CD\u5377\u53F7\uFF1A");
		lblNewLabel_8.setFont(new Font("宋体", Font.PLAIN, 14));
		lblNewLabel_8.setBounds(210, 310, 98, 48);
		frmv.getContentPane().add(lblNewLabel_8);
		
		lblNewLabel_9 = new JLabel("");
		lblNewLabel_9.setFont(new Font("宋体", Font.BOLD, 24));
		lblNewLabel_9.setForeground(Color.RED);
		lblNewLabel_9.setBounds(295, 310, 153, 48);
		frmv.getContentPane().add(lblNewLabel_9);
		
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 18));
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				String fjh = JedisUtil.get(Constants.juanhao+textField.getText()+":"+Constants.fenjuanhao_max+machine_id);
				if(StrUtil.isBlank(fjh)) {
					textField_1.setText("1");
				} else {
					int max = Integer.parseInt(fjh)+1;
					textField_1.setText(max+"");
				}
				textField_2.setText("15.2");
				textField_4.setText("");
				lblNewLabel_9.setText(textField.getText());
			}
		});
		textField.setBounds(268, 27, 100, 40);
		panel.add(textField);
		textField.setColumns(10);
		
		String jh = JedisUtil.get(Constants.juanhao+machine_id);
		textField.setText(jh);
		lblNewLabel_9.setText(jh);
		
		txt1 = new JTextField();
		txt1.setBounds(81, 27, 102, 40);
		txt1.setText(DateUtil.format(new Date(), "yyyy-MM-dd"));
		// 定义日历控件面板类
		CalendarPanel p = new CalendarPanel(txt1, "yyyy-MM-dd");
		p.initCalendarPanel();
		
		panel.add(p);
		panel.add(txt1);

		//卷号：
		JLabel lblNewLabel_1 = new JLabel("\u5377\u53F7\uFF1A");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setBounds(216, 30, 42, 33);
		panel.add(lblNewLabel_1);

		//分卷号：
		JLabel lblNewLabel_2 = new JLabel("\u5206\u5377\u53F7\uFF1A");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_2.setBounds(391, 30, 54, 37);
		panel.add(lblNewLabel_2);
		
		textField_1 = new JTextField();
		textField_1.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_1.setBounds(455, 27, 100, 40);
		panel.add(textField_1);
		textField_1.setColumns(10);
		String fjh = JedisUtil.get(Constants.juanhao+textField.getText()+":"+Constants.fenjuanhao_max+machine_id);
		if(!StrUtil.isBlank(fjh)) {
			int fjhInt = Integer.parseInt(fjh);
			textField_1.setText(String.valueOf(fjhInt+1));
		} else {
			textField_1.setText("1");
		}
		//规格：
		JLabel lblNewLabel_3 = new JLabel("\u89C4\u683C\uFF1A");
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_3.setBounds(585, 26, 53, 41);
		panel.add(lblNewLabel_3);
		
		textField_2 = new JTextField();
		textField_2.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_2.setText("15.2");
		textField_2.setBounds(648, 29, 100, 40);
		panel.add(textField_2);
		textField_2.setColumns(10);

		//长度：
		JLabel lblNewLabel_4 = new JLabel("\u957F\u5EA6\uFF1A");
		lblNewLabel_4.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_4.setBounds(10, 132, 69, 31);
		panel.add(lblNewLabel_4);
		
		textField_4 = new JTextField();
		textField_4.setEnabled(false);
		textField_4.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
			}
		});
		textField_4.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_4.setBounds(81, 123, 100, 40);
		panel.add(textField_4);

		//操作员：
		lblNewLabel_1_1 = new JLabel("\u64CD\u4F5C\u5458\uFF1A");
		lblNewLabel_1_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1_1.setBounds(204, 128, 54, 35);
		panel.add(lblNewLabel_1_1);
		
		textField_5 = new JTextField();
		textField_5.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_5.setBackground(SystemColor.inactiveCaptionBorder);
		textField_5.setEnabled(false);
		textField_5.setColumns(10);
		textField_5.setBounds(268, 123, 100, 40);
		panel.add(textField_5);

		//班次：
		JLabel lblNewLabel_2_1 = new JLabel("\u73ED\u6B21\uFF1A");
		lblNewLabel_2_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_2_1.setBounds(391, 128, 54, 35);
		panel.add(lblNewLabel_2_1);
		
		textField_6 = new JTextField();
		textField_6.setHorizontalAlignment(SwingConstants.CENTER);
		textField_6.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_6.setBackground(SystemColor.inactiveCaptionBorder);
		textField_6.setEnabled(false);
		textField_6.setColumns(10);
		textField_6.setBounds(455, 123, 100, 40);
		int hour = DateUtil.date().hour(true);
		if(hour<=17) {
			textField_6.setText("白班");
		} else {
			textField_6.setText("夜班");
		}
		panel.add(textField_6);
		
		
		scrollPane.setBounds(7, 368, 1292, 359);
		frmv.getContentPane().add(scrollPane);
		
		//查询
		JButton btnNewButton_5_1 = new JButton("\u67E5\u8BE2");
		btnNewButton_5_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String juanhao = textField.getText();
				if(StrUtil.isBlank(juanhao)) {
					juanhao = "*";
				}
				dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":"+juanhao);
				
			}
		});
		btnNewButton_5_1.setBounds(501, 200, 100, 46);
		panel.add(btnNewButton_5_1);

		//重置
		JButton btnNewButton_5_1_1 = new JButton("\u91CD\u7F6E");
		btnNewButton_5_1_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				txt1.setText(DateUtil.format(new Date(), "yyyy-MM-dd"));
				textField.setText("");
				textField_1.setText("");
				textField_2.setText("15.2");
				textField_4.setText("");
				dataTable.reloadTable(null);
			}
		});
		btnNewButton_5_1_1.setBounds(635, 200, 94, 46);
		panel.add(btnNewButton_5_1_1);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(SystemColor.inactiveCaption);
		panel_1.setForeground(SystemColor.inactiveCaptionBorder);
		panel_1.setBounds(825, 31, 474, 269);
		frmv.getContentPane().add(panel_1);
		panel_1.setLayout(null);

		//仪表数据区
		JLabel lblNewLabel_5 = new JLabel("\u4EEA\u8868\u6570\u636E\u533A(kg)");
		lblNewLabel_5.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel_5.setBounds(10, 10, 99, 36);
		panel_1.add(lblNewLabel_5);

		//称重
		JButton btnNewButton = new JButton("\u79F0\u91CD");

		textField_3 = new JTextField();
		textField_3.setHorizontalAlignment(SwingConstants.CENTER);
		textField_3.setFont(new Font("宋体", Font.PLAIN, 40));
		textField_3.setText("0.00");
		textField_3.setForeground(Color.GREEN);
		textField_3.setBackground(Color.BLACK);
		textField_3.setBounds(29, 80, 400, 91);
		panel_1.add(textField_3);
		textField_3.setColumns(10);

		textField_3.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void removeUpdate(DocumentEvent e) {
				//dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":"+textField.getText());
            }
             
            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = null;
                try {
                    text = e.getDocument().getText(e.getDocument().getStartPosition().getOffset(), e.getDocument().getLength());
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
                
                double nowValue = 0;
				if(!StrUtil.isBlank(textField_3.getText())) {
					String weight = textField_3.getText();
					if("0.00".equals(weight)) {
						weight = "0";
					}
					nowValue = Integer.parseInt(weight);
				}
				if(isStable(nowValue)) {
					btnNewButton.setEnabled(true);
				} else {
					btnNewButton.setEnabled(true);
				}
            }
             
            @Override
            public void changedUpdate(DocumentEvent e) {
				//dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":"+textField.getText());
            }
		});

		JPanel panel_3 = new JPanel();
		panel_3.setBackground(new Color(240, 248, 255));
		panel_3.setBounds(10, 737, 1289, 96);
		frmv.getContentPane().add(panel_3);
		panel_3.setLayout(null);


		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//输入长度
				LengthInputSwing lengthInputSwing = new LengthInputSwing(textField_4);
				lengthInputSwing.setVisible(true);
				lengthInputSwing.setAlwaysOnTop(true);

			}
		});
		btnNewButton.setBounds(521, 30, 117, 38);
		btnNewButton.setEnabled(false);
		panel_3.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("1#");
		JButton btnNewButton_1_1 = new JButton("2#");
		JButton btnNewButton_1_1_1 = new JButton("3#");
		JButton btnNewButton_1_1_1_1 = new JButton("4#");
		btnNewButton_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				machine_id = 1;
				txt1.setText(DateUtil.format(new Date(), "yyyy-MM-dd"));
				
				btnNewButton_1_1_1_1.setBackground(new Color(240,240,240));
				btnNewButton_1_1_1.setBackground(new Color(240,240,240));
				btnNewButton_1_1.setBackground(new Color(240,240,240));
				btnNewButton_1.setBackground(Color.orange);
				
				lblNewLabel_7.setText(machine_id+"#");
				
				String jh = JedisUtil.get(Constants.juanhao+machine_id);
				String fjh = JedisUtil.get(Constants.juanhao+jh+":"+Constants.fenjuanhao_max+machine_id);
				if(!StrUtil.isBlank(fjh)) {
					int fjhInt = Integer.parseInt(fjh);
					textField_1.setText(String.valueOf(fjhInt+1));
				} else {
					textField_1.setText("1");
				}
				textField.setText(jh);
				lblNewLabel_9.setText(jh);
				dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":*");
			}
		});
		btnNewButton_1.setBounds(10, 10, 83, 29);
		btnNewButton_1.setBackground(Color.orange);
		panel_3.add(btnNewButton_1);
		
		
		btnNewButton_1_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnNewButton_1_1.setBackground(Color.orange);
				txt1.setText(DateUtil.format(new Date(), "yyyy-MM-dd"));
				machine_id = 2;
				btnNewButton_1_1_1_1.setBackground(new Color(240,240,240));
				btnNewButton_1_1_1.setBackground(new Color(240,240,240));
				btnNewButton_1.setBackground(new Color(240,240,240));
				lblNewLabel_7.setText(machine_id+"#");
				
				String jh = JedisUtil.get(Constants.juanhao+machine_id);
				String fjh = JedisUtil.get(Constants.juanhao+Constants.fenjuanhao_max+machine_id);
				if(!StrUtil.isBlank(fjh)) {
					int fjhInt = Integer.parseInt(fjh);
					textField_1.setText(String.valueOf(fjhInt+1));
				} else {
					textField_1.setText("1");
				}
				textField.setText(jh);
				lblNewLabel_9.setText(jh);
				dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":*");
			}
		});
		btnNewButton_1_1.setBounds(129, 10, 83, 29);
		panel_3.add(btnNewButton_1_1);
		
		
		btnNewButton_1_1_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				machine_id = 3;
				txt1.setText(DateUtil.format(new Date(), "yyyy-MM-dd"));
				btnNewButton_1_1_1_1.setBackground(new Color(240,240,240));
				btnNewButton_1_1_1.setBackground(Color.orange);
				btnNewButton_1_1.setBackground(new Color(240,240,240));
				btnNewButton_1.setBackground(new Color(240,240,240));
				lblNewLabel_7.setText(machine_id+"#");
				
				String jh = JedisUtil.get(Constants.juanhao+machine_id);
				String fjh = JedisUtil.get(Constants.juanhao+Constants.fenjuanhao_max+machine_id);
				if(!StrUtil.isBlank(fjh)) {
					int fjhInt = Integer.parseInt(fjh);
					textField_1.setText(String.valueOf(fjhInt+1));
				} else {
					textField_1.setText("1");
				}
				textField.setText(jh);
				lblNewLabel_9.setText(jh);
				dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":*");
			}
		});
		btnNewButton_1_1_1.setBounds(245, 10, 83, 29);
		panel_3.add(btnNewButton_1_1_1);
		
		
		btnNewButton_1_1_1_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				machine_id = 4;
				txt1.setText(DateUtil.format(new Date(), "yyyy-MM-dd"));
				btnNewButton_1_1_1_1.setBackground(Color.orange);
				btnNewButton_1_1_1.setBackground(new Color(240,240,240));
				btnNewButton_1_1.setBackground(new Color(240,240,240));
				btnNewButton_1.setBackground(new Color(240,240,240));
				lblNewLabel_7.setText(machine_id+"#");
				
				String jh = JedisUtil.get(Constants.juanhao+machine_id);
				String fjh = JedisUtil.get(Constants.juanhao+Constants.fenjuanhao_max+machine_id);
				if(!StrUtil.isBlank(fjh)) {
					int fjhInt = Integer.parseInt(fjh);
					textField_1.setText(String.valueOf(fjhInt+1));
				} else {
					textField_1.setText("1");
				}
				textField.setText(jh);
				lblNewLabel_9.setText(jh);
				dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":*");
			}
		});
		btnNewButton_1_1_1_1.setBounds(357, 10, 93, 29);
		panel_3.add(btnNewButton_1_1_1_1);

		//提交
		JButton btnNewButton_3 = new JButton("\u63D0\u4EA4");
		btnNewButton_3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String token = JedisUtil.get(Constants.user+textField_5.getText());
				if(StrUtil.isBlank(token)) {
					LoginSwing ls = new LoginSwing(txt1.getText(),textField_5,textField.getText());
					ls.setVisible(true);
					ls.setAlwaysOnTop(true);
				} else {
					if(post(txt1.getText(),textField_5.getText())) {
						JOptionPane.showMessageDialog(null, "上传成功！");
						dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":*");
					}
				}
				
			}
		});
		btnNewButton_3.setBounds(708, 30, 117, 38);
		panel_3.add(btnNewButton_3);



		//换班
		JButton btnNewButton_4 = new JButton("\u6362\u73ed");
		btnNewButton_4.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ChangeLoginSwing changeLoginSwing = new ChangeLoginSwing(txt1.getText(), textField_5, textField.getText());
				changeLoginSwing.setVisible(true);
				changeLoginSwing.setAlwaysOnTop(true);
			}
		});
		btnNewButton_4.setBounds(890, 30, 117, 38);
		panel_3.add(btnNewButton_4);
		
		//设置menu
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(10, 0, 105, 35);
		frmv.getContentPane().add(menuBar);

		//系统
		JMenu mnNewMenu = new JMenu("\u7CFB\u7EDF");
		menuBar.add(mnNewMenu);

		//加重范围维护
		JMenuItem mntmNewMenuItem = new JMenuItem("\u52A0\u91CD\u8303\u56F4\u7EF4\u62A4");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				AddWeightSetting frame = new AddWeightSetting();
				frame.setVisible(true);
				frame.setAlwaysOnTop(true);
			}
		});

		mnNewMenu.add(mntmNewMenuItem);
	}
	
	private static Integer addweight(Integer weight) {
		List<String> addSettingList = JedisUtil.getList(Constants.addweightSetting);
		if(addSettingList!=null&&addSettingList.size()>=0) {
			for(String s:addSettingList) {
				AddWeightBean add = JSON.toJavaObject(JSON.parseObject(s), AddWeightBean.class);
				if(add!=null) {
					if(weight>=Integer.parseInt(add.getStart())&&weight<=Integer.parseInt(add.getEnd())) {
						return weight + Integer.parseInt(add.getWeightIncrease());
					}
				}
			}
		}
		return weight;
	}
	
	private boolean isStable(double value) {
		double total = 0;
		if(datalst.size()>=10) {
			datalst.remove(0);
		}
		datalst.add(value);
		if(datalst.size()>=10) {
			for(double v:datalst) {
				total+=v;
			}
			double dif = value-total/datalst.size();
			if(dif>=-20&&dif<=20) {
				return true;
			}
		}
		return false;
	}
	
	private boolean post(String date,String username) {
		Set<String> keys = JedisUtil.keys(date + ":weight:*");
		if(keys!=null&&keys.size()>0) {
			for(String key:keys) {
				List<String> jsonList = JedisUtil.getList(key);
				List<LtProductQualityBasic> pqbList = new ArrayList<LtProductQualityBasic>();
				List<LtProductQualityBasic> allPqbList = new ArrayList<LtProductQualityBasic>();
				if(jsonList!=null) {
					for(int i=0;i<jsonList.size();i++) {
		        		String json = jsonList.get(i);
		        		LtProductQualityBasic pqb = JSON.toJavaObject(JSON.parseObject(json), LtProductQualityBasic.class);
		        		if("0".equals(pqb.getSendFlag())||pqb.getSendFlag()==null) {
		        			pqbList.add(pqb);
		        		}
		        		allPqbList.add(pqb);
		        	}
					//提交
					String token = JedisUtil.get(Constants.user+username);
					System.out.println(JSON.toJSONString(pqbList));
					if(pqbList.size()>0) {
						String result = BizHelper.postBatch(JSON.toJSONString(pqbList),token);
						System.out.println(result);
						handleResult(result,key,allPqbList);
					}
				}
			}
		}
		
		return true;
	}
	
	private void handleResult(String result,String key,List<LtProductQualityBasic> allPqbList) {
		JsonResult jr = JSON.toJavaObject(JSON.parseObject(result), JsonResult.class);
		if(jr!=null&&jr.isSuccess()) {
			List<LtProductQualityBasic> rsPqbList = jr.getResult();
			if(allPqbList!=null) {
				for(LtProductQualityBasic pqb:allPqbList) {
					if("1".equals(pqb.getSendFlag())) {
						continue;
					}
					if(isExitSuccess(pqb,rsPqbList)) {
						pqb.setSendFlag("1");
					}else {
						pqb.setSendFlag("2");
					}
				}
				//
				JedisUtil.resetList(key, allPqbList);
			}
		}
	}
	
	private boolean isExitSuccess(LtProductQualityBasic pqb,List<LtProductQualityBasic> rsPqbList) {
		if(rsPqbList!=null) {
			for(LtProductQualityBasic rsPqb:rsPqbList) {
				if(pqb.getVolumeNumber().equals(rsPqb.getVolumeNumber())&&pqb.getReelNumber().equals(rsPqb.getReelNumber())) {
					return rsPqb.isSuccess();
				}
			}
		}
		return false;
	}



	public static void weight(){
		String juanhao = textField.getText();
		String fenjuanhao = textField_1.getText();
		String length = textField_4.getText();
		if(StrUtil.isBlank(juanhao)) {
			JOptionPane.showMessageDialog(frmv, "請輸入卷號");
			return;
		}
		if(StrUtil.isBlank(fenjuanhao)) {
			JOptionPane.showMessageDialog(frmv, "請輸入分卷號");
			return;
		}
		if(!Validator.isNumber(fenjuanhao)) {
			JOptionPane.showMessageDialog(frmv, "分卷號必須是數字");
			return;
		}

		if(StrUtil.isBlank(length)) {
			JOptionPane.showMessageDialog(frmv, "請輸入長度");
			return;
		}
		if(!Validator.isNumber(length)) {
			JOptionPane.showMessageDialog(frmv, "長度必須是數字");
			return;
		}
		//設置
		JedisUtil.set(Constants.juanhao+machine_id, juanhao);
		JedisUtil.set(Constants.juanhao+textField.getText()+":"+Constants.fenjuanhao_max+machine_id, fenjuanhao);



		/*
		 * WeightingProduct frame = new
		 * WeightingProduct(textField.getText(),textField_1.getText(),textField_3.
		 * getText()); frame.setVisible(true);
		 */

		String weight = textField_3.getText();
		String spec = textField_2.getText();
		LtProductQualityBasic pqb = new LtProductQualityBasic();
		pqb.setCreateDate(txt1.getText());
		pqb.setReelNumber(juanhao);
		pqb.setVolumeNumber(fenjuanhao);
		Integer dweight = Integer.parseInt(weight);

		pqb.setWeight(addweight(dweight));
		pqb.setLength(Integer.parseInt(length));
		pqb.setSpecs(spec);
		pqb.setSendFlag("0");
		//判斷卷號
		boolean flag = BizHelper.isExitFenjuanhao(txt1.getText()+":"+Constants.weight+":"+juanhao,juanhao, fenjuanhao);
		if(flag) {
			JOptionPane.showMessageDialog(frmv, "卷号分卷號已經存在！");return;
		}
		JedisUtil.lpush(txt1.getText()+":"+Constants.weight+":"+juanhao, JSON.toJSONString(pqb));
		
		//XinhuaApplication.dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":*");
		
		textField_1.setText((Integer.parseInt(fenjuanhao)+1)+"");
		JOptionPane.showMessageDialog(frmv, "稱重添加成功！");
		textField_4.setText("");//长度清空

		dataTable.reloadTable(txt1.getText()+":"+Constants.weight+":"+textField.getText());

	}

}
